package com.ayuku.commons.ui.usergroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.ConstClientServer;
import com.ayuku.commons.FormDialogBasic;
import com.ayuku.commons.Util;
import com.ayuku.commons.UtilPojo;
import com.ayuku.commons.business.appgroup.AppGroupImpl;
import com.ayuku.retail.persistence.model.AppGroup;
import com.ayuku.retail.persistence.modelmanual.VGroupFunctions;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class FormAppGroup extends FormDialogBasic {
	private CompUserGroup cmp;
	private String fGroupId;
	private String fGroupDesc;
	private Map<String, Object> mp;
	private AppGroup pjo;

	public FormAppGroup(final Shell parent, final int style, final int mode) {
		super(parent, style, mode);
	}

	@Override
	public void getPanelContent(final Group grpData) {
		cmp = new CompUserGroup(grpData, SWT.NONE);
	}

	@Override
	public void additionalProses() {
		getDialogShell().setText("Add/Edit Group");
		getDialogShell().setSize(520, 600);
		Util.setCenter(getDialogShell());

	}

	@Override
	public void clearContols() {
		cmp.fGroupID.setText("");
		cmp.fGroupDesc.setText("");
	}

	public void doLoadAdd(String str) {
		setList(0, null);
	}

	@Override
	public void doLoad(String key) throws Exception {
		Map<String, Object> mp = AppGroupImpl.getById(key);
		pjo = (AppGroup) mp.get(ConstClientServer.STR_HDR);

		if (pjo != null) {
			fGroupId = pjo.getId();
			cmp.fGroupID.setText(pjo.getId());
			cmp.fGroupDesc.setText(pjo.getName());
			setList(1, pjo.getId());
		}

	}

	@Override
	public boolean doValidate() {
		fGroupId = cmp.fGroupID.getText();
		if (Util.isEmpty(fGroupId)) {
			Util.showMsgBox("Kode harus diisi!", 1, getDialogShell());
			cmp.fGroupID.setFocus();
			return false;
		}

		if (fGroupId != null && fGroupId.equals("master")) {
			Util.showMsgBox("Cannot add/modify Master!", 1, getDialogShell());
			cmp.fGroupID.setFocus();
			return false;
		}

		fGroupDesc = cmp.fGroupDesc.getText();
		if (Util.isEmpty(fGroupDesc)) {
			Util.showMsgBox("Nama harus diisi!", 1, getDialogShell());
			cmp.fGroupDesc.setFocus();

			return false;
		}

		return true;
	}

	@Override
	public void enabledControl(final boolean otherField, final boolean keyField) {
		cmp.fGroupID.setEditable(keyField);
		cmp.fGroupDesc.setEditable(otherField);

	}

	@Override
	public int doMainInsert() throws Exception {
		setPojoWithData();
		return AppGroupImpl.add(mp);
	}

	@Override
	public int doMainDelete() throws Exception {
		int rtn = 0;
		if (pjo.getId() != null && !pjo.getId().equals(1)) {
			rtn = AppGroupImpl.delete(pjo.getId());
		}
		return rtn;
	}

	@Override
	public int doMainUpdate() throws Exception {
		setPojoWithData();
		return AppGroupImpl.update(mp);
	}

	private void setList(int mode, String groupId) {
		cmp.tblAuthority.removeAll();
		try {
			Util.createAssign(cmp.tblAuthority, mode, groupId);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	@Override
	protected void setPojoWithData() throws Exception {
		mp = new HashMap<String, Object>();
		pjo = new AppGroup();
		pjo.setId(fGroupId);
		pjo.setName(fGroupDesc);
		UtilPojo.setDateAndUser(pjo);
		mp.put(ConstClientServer.STR_HDR, pjo);

		GridItem[] items = cmp.tblAuthority.getItems();
		List<VGroupFunctions> lst = new ArrayList<VGroupFunctions>();

		for (int j = 0; j < items.length; j++) {
			VGroupFunctions func = new VGroupFunctions();
			func.setId(items[j].getText(0));
			func.setName(items[j].getText(1));
			func.setView(items[j].getChecked(2) + "");
			func.setAdd(items[j].getChecked(3) + "");
			func.setEdit(items[j].getChecked(4) + "");
			func.setDelete(items[j].getChecked(5) + "");
			lst.add(func);
		}
		mp.put(ConstClientServer.STR_DTL, lst);

	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

}
